import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant'
const isAuthenticated = localStorage.getItem('token')
const Home = () => import('../views/Home')

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  if (to.name != 'Home' && !isAuthenticated) {
    Toast.fail('未登录')
    next({ name: 'Home'})
  } else {
    next()
  }
})

export default router
